﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.WindowsEx;

namespace Krile.Forms.Dialogs.Editor
{
    public partial class QuickFeedback : Form
    {
        public QuickFeedback()
        {
            InitializeComponent();
        }

        private void urlOpen_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Subsystem.CommonOperation.CallBrowser("http://starwing.net");
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(nameBox.Text))
            {
                MessageBoxEx.Show("Please fill name text.", "Warning", MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Warning);
                return;
            }
            if (String.IsNullOrEmpty(bodyBox.Text))
            {
                MessageBoxEx.Show("Please fill body text.", "Warning", MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Warning);
                return;
            }
            nameBox.Enabled = false;
            bodyBox.Enabled = false;
            okBtn.Enabled = false;
            cancelBtn.Enabled = false;
            okBtn.Text = "Wait...";
            string err;
            PostString(new Uri("http://starwing.net/krile_quickfeedback.php"), nameBox.Text, bodyBox.Text, out err);
            if (!String.IsNullOrEmpty(err))
                MessageBoxEx.Show("Sending error has occured.", err, "Send error", MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Error);
            this.Close();
        }


        private bool PostString(Uri target, string name, string data, out string err)
        {
            err = null;
            //文字コードを指定する
            System.Text.Encoding enc = Encoding.Unicode;

            //POST送信する文字列を作成
            string postData = "text=" + Uri.EscapeDataString(data) + "&name=" + Uri.EscapeDataString(name);

            //バイト型配列に変換
            byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData);

            //WebRequestの作成
            System.Net.WebRequest req = System.Net.WebRequest.Create(target);

            req.Proxy = System.Net.WebRequest.DefaultWebProxy;

            //メソッドにPOSTを指定
            req.Method = "POST";
            //ContentTypeを"application/x-www-form-urlencoded"にする
            req.ContentType = "application/x-www-form-urlencoded";
            //POST送信するデータの長さを指定
            req.ContentLength = postDataBytes.Length;

            try
            {
                //データをPOST送信するためのStreamを取得
                System.IO.Stream reqStream = req.GetRequestStream();
                //送信するデータを書き込む
                reqStream.Write(postDataBytes, 0, postDataBytes.Length);
                reqStream.Close();

                //サーバーからの応答を受信するためのWebResponseを取得
                System.Net.WebResponse res = req.GetResponse();
                //応答データを受信するためのStreamを取得
                System.IO.Stream resStream = res.GetResponseStream();
                //受信して表示
                System.IO.StreamReader sr = new System.IO.StreamReader(resStream, enc);
                //閉じる
                sr.Close();
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }
    }
}
